<?php
class Createquestion extends CI_Controller{
    public function __construct(){
        parent::__construct();
    }
    public function index(){
       

        $this->load->Model("backend/Mcreateproject");
        $arr_project = $this->Mcreateproject->getAllProject();
        $temp['data']['project'] = $arr_project;

       // var_dump($temp);
        $this->load->view("backend/create_project_question", $temp);

    }


    public function addquestion(){
            $id_project = $this->input->post("id");

            $add = array(
                    "content" => $this->input->post("question"),
                    "id_project"    =>  $id_project,
                    
                 );

            $this->load->Model("backend/Mcreatequestion");
            $id = $this->Mcreatequestion->insert_question($add);
            if($id>0)
            {

                $q1= $this->input->post("q1");
                $q2= $this->input->post("q2");
                $q3= $this->input->post("q3");
                if($q1 != "")
                {
                  $addanswer = array(
                    "id_question" => $id,
                    "id_project"    =>  $id_project,
                    "content"    => $q1,
                    
                 );
                  $this->load->Model("backend/Mcreatequestion");
                  //gia tri tra ve la true
                  if($this->Mcreatequestion->insert_answer($addanswer))
                  {
                    //neu insert cau hoi thanh cong   
                    echo "ok";
                  }
                }

                 if($q2 != "")
                {
                  $addanswer = array(
                    "id_question" => $id,
                    "id_project"    =>  $id_project,
                    "content"    => $q2,
                    
                 );
                  $this->load->Model("backend/Mcreatequestion");
                  //gia tri tra ve la true
                  if($this->Mcreatequestion->insert_answer($addanswer))
                  {
                    //neu insert cau hoi thanh cong   
                    echo "ok";
                  }
                }

                 if($q3 != "")
                {
                  $addanswer = array(
                    "id_question" => $id,
                    "id_project"    =>  $id_project,
                    "content"    => $q3,
                    
                 );
                  $this->load->Model("backend/Mcreatequestion");
                  //gia tri tra ve la true
                  if($this->Mcreatequestion->insert_answer($addanswer))
                  {
                    //neu insert cau hoi thanh cong   
                    echo "ok";
                  }
                }
            }
            else{echo "not nok";}
    }

//list danh sach cac project
   public function getproject(){
        $this->load->Model("backend/Mcreateproject");
        $arr_project = $this->Mcreateproject->getAllProject();
        $temp['data']['project'] = $arr_project;

       // var_dump($temp);
        $this->load->view("backend/create_project", $temp);

   }   

   public function getprojectbyid(){

        $id =$_GET['id'];;
        $this->load->Model("backend/Mcreatequestion");

        
        $arr_project = $this->Mcreatequestion->getquestionbyid($id);

        $temp['data'] = $arr_project;

        foreach ($temp['data'] as $subcate)
            {
              echo( "<option value='".$subcate['id']."''>".$subcate['content']."</option>");
            
            }
        //tra ve json 
        //echo(json_encode($arr_project));
   }    

 public function getanswerbyid(){

        $id =$_GET['id'];;
        $this->load->Model("backend/Mcreatequestion");
        $arr_project = $this->Mcreatequestion->getanswerbyid($id);
        $temp['data'] = $arr_project;

        //var_dump( $temp['data'] );
        //$temp['data']['project'] = $arr_project;
        //tra ve json 
        echo(json_encode($temp['data']));
   }    



     public function updateproject(){

        $name_project = $this->input->post('full_name');
        $video_project = $this->input->post('linkvideo');
        $id = $this->input->post('id');


        $params = array(
          'id' => $id,
          'name_project' =>  $name_project,
          'video_project' =>  $video_project,
        );      

        $this->load->Model("backend/Mcreateproject");
        $result = $this->Mcreateproject->update_ProjectByID($params);
        if($result=1){
            echo "done";
        }
        else{
            echo "notdone";
        }

     }
}